package de.fhkl.android.utils;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ExpandableListViewFactory {
	private ArrayList<View> group = new ArrayList<View>();
	private ArrayList<ArrayList<View>> child=new ArrayList<ArrayList<View>>();;
	private ExpandableListView root;
	private Context context;
	private String tag = "ExpandableListViewFactory";

	/**
	 * Constructor,will create a ExpadableListView.
	 * 
	 * @param context
	 */
	public ExpandableListViewFactory(Context context) {
		this.context = context;
		createRoot(this.context);
	}

	/**
	 * Create a ExpandableListView as root
	 * 
	 * @param context
	 */
	private void createRoot(Context context) {
		this.root = new ExpandableListView(context);
	}

	/**
	 * Add a View as new group into root.
	 * 
	 * @param g
	 */
	public void addGroup(View g) {
		this.group.add(g);
		this.child.add(new ArrayList<View>());
	}

	/**
	 * Add a View as child into the certain group
	 * 
	 * @param g
	 *            The target group
	 * @param c
	 *            The child to add.
	 */
	public void addChild(View g, View c) {
		int groupIndex = group.indexOf(g);
		if (groupIndex >= 0) {
			child.get(groupIndex).add(c);
		}
	}

	public ExpandableListView getRoot() {
		root.setAdapter(new MenuListAdapter(context));
		return root;
	}

	class MenuListAdapter extends BaseExpandableListAdapter {
		public MenuListAdapter(Context context) {
		}

		public Object getChild(int groupPosition, int childPosition) {
			return child.get(groupPosition).get(childPosition);
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View c = child.get(groupPosition).get(childPosition);
			return getGenericView(c);
		}

		public int getChildrenCount(int groupPosition) {
			return child.get(groupPosition).size();
		}

		// -------------Group-----------//
		public Object getGroup(int groupPosition) {
			return group.get(groupPosition);
		}

		public int getGroupCount() {
			return group.size();
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View g = group.get(groupPosition);
			return getGenericView(g);
		}

		// --------------------Create view----------------//
		public View getGenericView(View g) {
			View view = g;
			return view;
		}

		public boolean hasStableIds() {
			return false;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return false;
		}
	}
}
